package com.maniu.h264maniu;

import android.Manifest;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    H264Player h264Player;

    public boolean checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, 1);

        }
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();
        initSurface();
    }

    //SurfaceView  画框   Surface 画布
//    画师   准备
    private void initSurface() {
        SurfaceView surface = (SurfaceView) findViewById(R.id.preview);
        surface.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
                Surface surface1 = surfaceHolder.getSurface();
                byte[] data = readVideoFromAsset();
//                h264Player = new H264Player(
//                        new File(Environment.getExternalStorageDirectory(), "splice1.h264")
//                                .getAbsolutePath()
//                        , surface1);
//                h264Player = new H264Player(data, surface1);
//                h264Player.play();
                H265Player  h265Player = new H265Player(data, surface1);
                h265Player.play();
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

            }
        });
    }

    private byte[] readVideoFromAsset() {
        AssetManager am = getAssets();
        InputStream inputStream = null;
        ByteArrayOutputStream bos = null;
        int len;
        int size = 1024;
        byte[] buf;
        try {
            inputStream = am.open("output.h265");
            bos = new ByteArrayOutputStream();
            buf = new byte[size];
            while ((len = inputStream.read(buf, 0, size)) != -1)
                bos.write(buf, 0, len);
            buf = bos.toByteArray();
            return buf;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}